package com.cn.qgx428.lazydiary.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
 * Created by qinguanxiong on 2018/1/14.
 */
abstract class RAdapter<T>: RecyclerView.Adapter<RViewHolder> {

    lateinit var mContext : Context
    private var layoutId : Int = -1
    private  var datas :MutableList<T> = mutableListOf()

    constructor(context: Context, layoutId: Int = -1, datas:MutableList<T> = mutableListOf()){
        mContext = context
        this.layoutId = layoutId
        this.datas = datas
    }

    override fun onBindViewHolder(holder: RViewHolder?, position: Int) {
        val positionInParent =
        conver(holder!!, datas.get(position))
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RViewHolder {
        val view : View = LayoutInflater.from(mContext).inflate(layoutId,parent,false)
        return RViewHolder(view)
    }

    abstract fun conver(holder: RViewHolder, data: T)

    open fun setDatas(datas: MutableList<T>){
        this.datas = datas
        notifyDataSetChanged()
    }

    fun getDatas() = datas

    fun appendDatas(moreDates: MutableList<T>){

        datas.addAll(moreDates)


        notifyDataSetChanged()
    }

    fun removeAt(indext: Int){
        datas.removeAt(indext)
        notifyDataSetChanged()
    }

    fun setLayoutId(id: Int)  {layoutId = id}
}